from google.appengine.ext import db
import time
import datetime
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app


print 'Content-Type: text/plain'
print ''
print 'Database Demo: %s' % time.asctime()

class MainHandler(webapp.RequestHandler):
    def get(self):
      person = Student('George', 'Alabama', '12-12-12')
      template_values = {'person': person}
      path = os.path.join(os.path.dirname(__file__), 'dbdemo.html')
      self.response.out.write(template.render(path, template_values))

class Student(db.Model):
  name = db.StringProperty(required=True)
  home_state = db.StringProperty(required=True)
  birthday = db.StringProperty(required=False)
  ts = db.DateTimeProperty(auto_now_add=True)

def CreateStudent(name, home_state, birthday=None):
  print 'Creating student: %s' %(name)
  student = Student(name=name, home_state=home_state, birthday=birthday)
  student.put()

def GetAllStudents():
  return Student.all()
  
def PrintStudents(query):
  print 'Name\t\tHome\t\tBirthday\t\tDate_added'
  for r in query:
    print '%s\t\t%s\t\t%s\t\t%s' %(r.name, r.home_state, r.birthday, r.ts)


def PrintAllStudents():
  print 'Listing all students in the Datastore'
  query = GetAllStudents()
  PrintStudents(query)

def FindStudentsInState(state):
  query = Student.all()
  query.filter("home_state =", state)
  return query

def ResetStudents():
  print 'Resetting Students'
  query = Student.all()
  if query:
    for r in query:
      r.delete()
  print ''

def MoveStudents(from_state, to_state):
  print 'Moving all students from: %s to %s' %(from_state, to_state)
  query = Student.all()
  query.filter("home_state =", from_state)
  for r in query:
    r.home_state = to_state
    r.put()

def GetNStudents(n=10):
  print 'Getting first %s students' %(n)
  query = Student.all()
  return query.fetch(n)

def GetStudentRange(limit=1, offset=1):
  print 'Getting %d students from position %d' %(limit, offset)
  query = Student.all()
  return query.fetch(limit=limit, offset=offset)

def Reincarnation(old_birthday):
  print 'You thought these people were born on %s, but they were just reincarnated' %(old_birthday)
  query = Student.all()
  query.filter("birthday =", old_birthday)
  for r in query:
    r.birthday = str(datetime.datetime.now())
    r.put()

def FindStudentsWithBirthday(bday):
  query = Student.all()
  query.filter("birthday =", bday)
  return query

print ''
ResetStudents()
CreateStudent('Freddie Bob', 'California', '12-14-1993')
CreateStudent('Cassie', 'Canada', '12-14-1993')
CreateStudent('Ayesha', 'California', '05-15-1993')
CreateStudent('Hannah', 'New York', '07-28-1993')
CreateStudent('Nora', 'Kentucky', '11-17-1992')
print ''
PrintAllStudents()
print ''
print 'Now finding students in California'
PrintStudents(FindStudentsInState('California'))
print ''
MoveStudents('California', 'Oregon')
print ''
PrintAllStudents()
print ''
PrintStudents(GetNStudents(2))

print ''
PrintStudents(GetStudentRange(2,1))
print ''
Reincarnation('11-17-1992')
print''
PrintAllStudents()
print ''
PrintStudents(FindStudentsWithBirthday('12-14-1993'))






def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  run_wsgi_app(application)




if __name__ == '__main__':
  main()

